﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BangleCMS.Model;
using BangleCMS.BLL;
using System.Linq;

public partial class System_News_NewsModify : BasePage
{
    t_news model = new t_news();
    t_newsBLL bll = new t_newsBLL();
    t_webnavBLL nav_bll = new t_webnavBLL();

    #region 属性

    /// <summary>
    /// Id
    /// </summary>
    private int Id
    {
        get
        {
            return base.ParseQueryInt("Id", 0);
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return base.ParseQueryString("Action", "Add");
        }
    }

    #endregion

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        //base.CheckUserPermission("001001");

        if (!IsPostBack)
        {
            BindDropdown();
            getDate(Id);
        }
    }
    #endregion

    #region 加载数据
    private void getDate(int id)
    {
        if (Action == "Update")
        {
            model = bll.GetModel(id);
            this.ddl_NewsType.SelectedValue = model.NewsType;
            this.txt_Title.Text = model.Title;
            this.txt_Keywords.Text = model.Keywords;
            this.txt_Description.Text = model.Description;
            this.txt_Source.Text = model.Source;
            this.txt_Author.Text = model.Author;
            //model.OrderNo = Convert.ToInt32(this.txt_OrderNo.Text.Trim());
            this.txt_Abstract.Text = model.Abstract;
            //this.Editor.Value = model.Content;
            this.Editor.Text = model.Content;
            this.txt_ReleaseDate.Text = model.ReleaseDate.ToString();
            this.radio_Valid.SelectedValue = model.Valid.ToString();
        }
        else
        {
            this.txt_ReleaseDate.Text = DateTime.Now.ToString();
            this.txt_Author.Text = base.CurrentSession.UserName;
            this.txt_Source.Text = new WebConfig().GetConfigValue("WebSideName");
        }
    }

    private void BindDropdown()
    {
        t_webnav nav = nav_bll.GetModelList(" Code='News' ").FirstOrDefault();
        List<t_webnav> list = nav_bll.GetModelList(string.Format(" ParentId={0} ", nav.WebNavId));
        foreach (t_webnav item in list)
        {
            item.Title = "├──" + item.Title;
        }
        this.ddl_NewsType.DataTextField = "Title";
        this.ddl_NewsType.DataValueField = "Code";
        this.ddl_NewsType.DataSource = list;
        this.ddl_NewsType.DataBind();
        this.ddl_NewsType.Items.Insert(0, new ListItem("├" + nav.Title, nav.Code));
    }
    #endregion

    #region 提交
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        if (Action == "Update")
        {
            #region 修改
            try
            {
                t_news model = new t_news();
                t_newsBLL bll = new t_newsBLL();

                model = bll.GetModel(Id);

                if (model == null)
                {
                    base.Alert("操作失败！");
                    return;
                }
                model.NewsType = this.ddl_NewsType.SelectedValue;
                model.Title = this.txt_Title.Text.Trim();
                model.Keywords = this.txt_Keywords.Text.Trim();
                model.Description = this.txt_Description.Text.Trim();
                model.Source = this.txt_Source.Text.Trim();
                model.Author = this.txt_Author.Text.Trim();
                //model.OrderNo = Convert.ToInt32(this.txt_OrderNo.Text.Trim());
                model.Abstract = this.txt_Abstract.Text.Trim();
                model.Content = this.Editor.Text;
                model.ReleaseDate = Convert.ToDateTime(this.txt_ReleaseDate.Text.Trim());
                model.Valid = Convert.ToInt32(this.radio_Valid.SelectedValue);

                model.CreateUser = base.CurrentSession.UserID;
                model.CreateDate = DateTime.Now;
                model.UpdateUser = model.CreateUser;
                model.UpdateDate = model.CreateDate;
                bll.Update(model);

                base.Redirect("操作成功！", "NewsList.aspx");
            }
            catch (Exception ex)
            {
                base.Alert("操作失败！");
                //throw ex;
            }
            #endregion
        }
        else
        {
            #region 新增
            try
            {
                t_news model = new t_news();
                t_newsBLL bll = new t_newsBLL();
                model.NewsType = this.ddl_NewsType.SelectedValue;
                model.Title = this.txt_Title.Text.Trim();
                model.Keywords = this.txt_Keywords.Text.Trim();
                model.Description = this.txt_Description.Text.Trim();
                model.Source = this.txt_Source.Text.Trim();
                model.Author = this.txt_Author.Text.Trim();
                //model.OrderNo = Convert.ToInt32(this.txt_OrderNo.Text.Trim());
                model.Abstract = this.txt_Abstract.Text.Trim();
                model.Content = this.Editor.Text;
                model.ReleaseDate = Convert.ToDateTime(this.txt_ReleaseDate.Text.Trim());
                model.Valid = Convert.ToInt32(this.radio_Valid.SelectedValue);

                //查看次数
                model.ReadCount = 0;

                model.CreateUser = base.CurrentSession.UserID;
                model.CreateDate = DateTime.Now;
                model.UpdateUser = model.CreateUser;
                model.UpdateDate = model.CreateDate;
                bll.Add(model);

                base.Redirect("操作成功！", "NewsList.aspx");
            }
            catch (Exception ex)
            {
                base.Alert("操作失败！ ex:" + ex.ToString());
                //throw ex;
            }
            #endregion
        }

    }
    #endregion
}
